package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> getList(UserDto userDto) {
        //分页插件
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());

        //获取列表
        List<User> list= userMapper.getList(userDto);

        //解析分页结果
        Page<User> pageList = (Page<User>) list;

        return new PageResult<>(pageList.getTotal(),pageList.getResult());

    }

    @Override
    public void addUser(User user) {
        //1\添加基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        //2\添加密码配置
        String password=user.getUsername()+"123";

        //2-2\密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        //3\调用Mapper
        userMapper.addUser(user);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        userMapper.deleteById(ids);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void updateById(User user) {
        user.setUpdateTime(LocalDateTime.now());

        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        userMapper.updateById(user);
    }

    @Override
    public List<User> getListUserByRoleId(String roleLabel) {

        return userMapper.getListUserByRoleId(roleLabel);
    }

    @Override
    public LoginResultVo login(User user) {
        //1\获取用户
        User userDB=userMapper.selectByUsername(user.getUsername());

        //2\验证用户是否存在
        if(userDB == null){
            log.info("用户不存在");
//            return null;
            throw new BusinessException("用户名或密码错误");
        }
        //3\判断密码是否正确
        String _pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!_pass.equals(userDB.getPassword())){
            log.info("密码错误");
//            return null;
            throw new BusinessException("用户名或密码错误");
        }

        //4\判断状态是否激活
        if(userDB.getStatus() != 1){
            log.info("用户未激活");
//            return null;
            throw new BusinessException("用户已停用");
        }

        //5\封装返回
        LoginResultVo loginResultVo=new LoginResultVo();
        loginResultVo.setId(userDB.getId());
        loginResultVo.setUsername(userDB.getUsername());
        loginResultVo.setName(userDB.getName());
        loginResultVo.setImage(userDB.getImage());
        loginResultVo.setRoleLabel(userDB.getRoleLabel());

        //调用工具类,生成jwt令牌
        Map<String,Object> claims=new HashMap<>();
        claims.put("id",userDB.getId());
        claims.put("username",userDB.getUsername());

        String jwt_token= JwtUtils.generateToken(claims);

        loginResultVo.setToken(jwt_token);

        return loginResultVo;
    }
}
